package cn.com.darly.vpm.mobile.session;

import cn.com.darly.vpm.framework.common.StringDiyUtils;
import cn.com.darly.vpm.security.user.bean.User;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Author : ZhangYuHui
 * Date : 2017/12/4
 * TODO :
 */
public class SessionUtil {
    /**
     * 记录登录用户map
     */
    public static Map<String, User> SESSION_MAP = new HashMap<>();

    /**
     * 记录用户最后操作时间的map
     */
    public static Map<String, Date> LAST_OPERATION_MAP = new HashMap<>();

    /**
     * 生成保存在集合中的键值
     * @param request
     * @return
     */
    public static String makeSessionKey(HttpServletRequest request) {
        String token = request.getHeader("usertoken");
        if(StringDiyUtils.isEmpty(token)){
            String username = request.getParameter("username");
            StringBuffer buffer = new StringBuffer();
            buffer.append("张宇辉专用TOKEN关键字");
            buffer.append(username);
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(buffer.toString().getBytes());
        }else {
            return token;
        }
    }
}
